#include "Button.h"

Button::Button( string caption, int x, int y )
{
    spr = ResourceManager::CreateAnimation("Button1", x,y);
    spr->SetSpriteFrame(0);

    m_Caption = caption;
    m_textWidth = dbTextWidth((char*)m_Caption.c_str());
    m_textHeight = dbTextHeight((char*)m_Caption.c_str());
}


Button::~Button()
{
    if (spr) delete spr;
}

void Button::OnDraw()
{
    RECT *r = GetRect();
    char *txt = (char*)m_Caption.c_str();
    int w = r->right  - r->left;
    int h = r->bottom - r->top;
    dbInk(RGB(0,0,0), RGB(0,0,0));
    dbText(r->left + (w - m_textWidth)/2,
        r->top  + (h - m_textHeight)/2,
        txt);
}

void Button::OnClick( int x, int y )
{
    spr->SetSpriteFrame(2);
    ClickEventArgs args(x,y);
    //this->SetListener(NULL);
    this->FireEvent(this, &args);    
}

RECT* Button::GetRect()
{
    m_Rect.left = (LONG)spr->XPos;
    m_Rect.top  = (LONG)spr->YPos;
    m_Rect.right = m_Rect.left + spr->GetWidth();
    m_Rect.bottom = m_Rect.top + spr->GetHeight();
    return &m_Rect;
}

void Button::OnHover( int x, int y )
{    
    spr->SetSpriteFrame(2);
}

void Button::OnLeave()
{
    spr->SetSpriteFrame(0);
}

TGDKSprite* Button::GetMainSprite()
{
    return spr;
}

int Button::GetWidth()
{
    return spr->GetWidth();
}

int Button::GetHeight()
{
    return spr->GetHeight();
}

void Button::SetPosition( int x, int y )
{
    spr->XPos = x;
    spr->YPos = y;
    spr->Refresh();
}
